package main.adapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hichip.campro.R;
import com.hichip.control.HiCamera;

import java.io.File;
import activity.Left.LeftGetDataModel;
import activity.Left.LeftLocalVideoPhotoActivity;
import activity.LocalFile.LocalFileActivity2;
import activity.setting.SettingActivity;
import activity.video.RemoteRecordingListActivity;
import bean.LeftLocalFileBean;
import bean.MyCamera;
import common.HiDataValue;
import main.MainActivity;
import utils.HiToast;
import utils.HiTools;
/**
 * Created by lt on 2017\12\29 0029.
 */
public class CameraListBigAdapter extends BaseAdapter {
    private Context mContext;

    public CameraListBigAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return HiDataValue.CameraList.size();
    }

    @Override
    public Object getItem(int position) {
        return HiDataValue.CameraList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       MyCamera camera = (MyCamera) getItem(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext  , R.layout.item_main_camera_big, null);
            holder.cardview=convertView.findViewById(R.id.cardview);
            holder.tv_nickname=convertView.findViewById(R.id.tv_nickname);
            holder.tv_uid = convertView.findViewById(R.id.tv_uid);
            holder.tv_camera_status = convertView.findViewById(R.id.tv_camera_status);
            holder.iv_snapshot = convertView.findViewById(R.id.iv_snapshot);
            holder.iv_video = convertView.findViewById(R.id.iv_video);
            holder.iv_photo = convertView.findViewById(R.id.iv_photo);
            holder.iv_setting = convertView.findViewById(R.id.iv_setting);
            holder.iv_status=convertView.findViewById(R.id.iv_status);
            holder.iv_small_bell=convertView.findViewById(R.id.iv_small_bell);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (camera.getAlarmState() == 0) {
            holder.iv_small_bell.setVisibility(View.GONE);
        } else {
            holder.iv_small_bell.setVisibility(View.VISIBLE);
        }

        holder.tv_nickname.setText(camera.getNikeName());
        holder.tv_uid.setText(camera.getUid());
        //设置摄像机状态
        HiTools.setCameraStatusBack(mContext,holder.iv_status,holder.tv_camera_status,camera.getConnectState());
        if (camera.isSystemState == 1 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
            holder.tv_camera_status.setText(mContext.getString(R.string.tips_restart));
        }
        if (camera.isSystemState == 2 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
            holder.tv_camera_status.setText(mContext.getString(R.string.tips_recovery));
        }
        if (camera.isSystemState == 3 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
            holder.tv_camera_status.setText(mContext.getString(R.string.tips_update));
        }
//        //设置摄像机状态
//        HiTools.setCameraStatusBack(mContext, holder.iv_status,holder.tv_camera_status,camera.getConnectState());
        if (camera.snapshot != null) {
            holder.iv_snapshot.setImageBitmap(camera.snapshot);
        }else {
            holder.iv_snapshot.setImageResource(R.mipmap.videoclip);
        }
        //设置
        LinearLayout ll_setting=convertView.findViewById(R.id.ll_setting);
        ViewHolder finalHolder = holder;
        ll_setting.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                if (camera != null) {
                    Intent intent = new Intent(mContext, SettingActivity.class);
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID, camera.getUid());
                    if (mContext != null && (mContext instanceof Activity)) {
                        Activity act = (Activity) mContext;
                        //转场动画设置
                        ActivityOptions options=ActivityOptions.makeSceneTransitionAnimation(act, finalHolder.iv_snapshot,"iv");
                        act.startActivity(intent,options.toBundle());
                    }
                }
            }
        });
        //录像回放
        LinearLayout ll_video = convertView.findViewById(R.id.ll_video);
        ll_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (camera != null&&camera.getConnectState()==HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                    Intent intent=new Intent(mContext, RemoteRecordingListActivity.class);
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID, camera.getUid());
                    mContext.startActivity(intent);
                    if (mContext != null && (mContext instanceof MainActivity)) {
                        MainActivity act = (MainActivity) mContext;
                        act.overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
                    }
                }else {
                    HiToast.showToast(mContext,mContext.getString(R.string.current_disconnect));
                }
            }
        });
        //该摄像机 本地文件
        LinearLayout ll_photo = convertView.findViewById(R.id.ll_photo);
        ll_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(camera!=null){
                    Intent intent=new Intent(mContext, LocalFileActivity2.class);
                    intent.putExtra(HiDataValue.EXTRAS_KEY_UID,camera.getUid());
                    mContext.startActivity(intent);
                    if (mContext != null && (mContext instanceof Activity)) {
                        Activity act = (Activity) mContext;
                        act.overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
                    }
                }
            }
        });
        return convertView;
    }


    private class ViewHolder {
        CardView cardview;
        TextView tv_nickname;
        TextView tv_uid;
        TextView tv_camera_status;
        ImageView iv_snapshot;
        ImageView iv_video;
        ImageView iv_photo;
        ImageView iv_setting;
        ImageView iv_status;
        ImageView iv_small_bell;
    }

    // 获取图片数量，
    public int getFileCount(MyCamera camera) {
        String path = HiDataValue.getPathCameraSnapshot(camera.getUid(),mContext);
        File folder = new File(path);
        String[] imageFiles = folder.list();
        if (imageFiles == null) {
            return 0;
        }
        int sum = 0;
        sum = imageFiles.length;
        return sum;
    }

}




